//
//  MYGSocketAPI.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/12.
//

import UIKit
import Moya


enum MYGSocketAPI: MYGTargetType {
    case uploadFile(image: UIImage?, data: Data? = nil, fileName: String = "", type: String = "image/*")
    case emoji
    case getLoginToken
    
}

extension MYGSocketAPI {
    var baseURL: URL {
        return URL(string: "https://im-dev.medataplus.com")!
//        return URL(unsafeString: "https://yihuanim.weincloud.com")
    }
    
    var path: String {
        switch self {
        case .uploadFile:
            "/addons/fastim/index/upload"
        case .emoji:
            "/addons/fastim/index/getEmoji"
        case .getLoginToken:
            "/addons/fastim/api.user/login"
        
        }
    }
    
    var task: Moya.Task {
        switch self {
        case .uploadFile(let image, let data, let fileName, let type):
            var formDatas = [MultipartFormData]()
            if let image = image {
                let imageData = image.compressImage(maxSize: 10*1024*1024-1)!
                let fileName = "\(Date().timeIntervalSince1970).png"
                let formData = MultipartFormData(provider: .data(imageData), name: "file", fileName: fileName, mimeType: "image/*")
                formDatas.append(formData)
            }
            
            if let data = data {
                let formData = MultipartFormData(provider: .data(data), name: "file", fileName: fileName, mimeType: type)
                formDatas.append(formData)
            }
            
            let token:String = (MYGSocketManager.shared.chatModel?.userinfo.token)!
//            let formData2 = MultipartFormData(provider: .data("7|864000|1712368716|94396adfc3bab612e072217948575e49".data(using: .utf8)!), name: "im_tourists_token")
            let formData2 = MultipartFormData(provider: .data(token.data(using: .utf8)!), name: "im_tourists_token")

            formDatas.append(formData2)
            return .uploadMultipart(formDatas)
        case .emoji:
            var params: [String: Any] = [:]
            params["im_tourists_token"] =  MYGSocketManager.shared.chatModel?.userinfo.token //"7|864000|1712368716|94396adfc3bab612e072217948575e49"
            return .requestParameters(parameters: params, encoding: URLEncoding.default)
            
        case .getLoginToken:
            var params: [String: Any] = [:]
            params["account"] = MYGUserManager.getUser()?.mobile
            params["role_type"] = 1
            return .requestParameters(parameters: params, encoding: URLEncoding.default)
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .uploadFile:
            .post
        case .emoji:
            .get
        case .getLoginToken:
            .post
        
        }
    }
    
    var isServer: Int {
        2
    }
}
